package edu.upf.da.p2p.sm.client.message;

import org.jivesoftware.smack.packet.Message;

import edu.upf.da.p2p.sm.P2PMessage;

public class AuthResponse extends P2PMessage {

	public static final int LOGIN_OK = 210;
	public static final int LOGIN_USER_NOT_FOUND = 410;
	public static final int LOGIN_INVALID_PASS = 411;
	public static final int LOGIN_BANNED_USER = 412;
	public static final int LOGIN_INTERNAL_ERROR = 510;

	public static final int LOGOUT_OK = 220;
	public static final int LOGOUT_INTERNAL_ERROR = 520;

	public AuthResponse(Message m){super(m);}

	private AuthResponse(){};

	public AuthResponse(int code){
		super();
		String op = (""+code).substring(1,2);
		if(op.equals("1")){
			setSubject("login");
		}else if (op.equals("2")){
			setSubject("logout");
		}
		setPacketID(code+"");
	}

	public int getErrorCode(){
		return Integer.parseInt(getPacketID());
	}

	public boolean isLogIn(){
		return getSubject().equals("login");
	}

	public boolean isLogOut(){
		return getSubject().equals("logout");
	}

	public boolean isOK(){
		if(getErrorCode() == LOGIN_OK || getErrorCode() == LOGOUT_OK)
			return true;
		else
			return false;
	}
}
